/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.security.MessageDigest;
import java.util.Random;

/**
 * <p>Project: javase - StringBuilderDemo
 * <p>Powered by webrx On 2021-12-06 09:43:58
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class StringBuilderDemo {
    public static void main(String[] args) {
        String str = "hello";
        StringBuilder sbu2 = new StringBuilder(str);
        sbu2.append(",Java");
        System.out.println(sbu2.reverse().toString());

        //md5 单项加密
        try {
            String pwd = "admin";
            System.out.println(pwd);
            MessageDigest md = MessageDigest.getInstance("md5");
            md.update(pwd.getBytes());
            StringBuilder sbu = new StringBuilder();
            for (byte b : md.digest()) {
                sbu.append(String.format("%02x", b));
            }
            //md5加密后
            System.out.println(sbu.toString());
            System.out.println(sbu.toString().length());
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println(getPass(20));
    }

    public static String getPass(int len) {
        //随机密码
        String letter = "~!@#$%^&*()_+{}|\":?><abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sbu = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < len; i++) {
            sbu.append(letter.charAt(rand.nextInt(letter.length())));
        }
        return sbu.toString();
    }
}
